package com.deyuanyun.pic.settings.service.greatwall.impl;

import com.deyuanyun.pic.settings.domain.greatwall.ProductLog;
import com.deyuanyun.pic.settings.domain.greatwall.ProductLogExample;
import com.deyuanyun.pic.settings.mapper.greatwall.ProductLogMapper;
import com.deyuanyun.pic.settings.service.greatwall.ProductLogService;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  产品日志service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-12-20
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class ProductLogServiceImpl implements ProductLogService {

    @Autowired
    private ProductLogMapper productLogMapper;

    @Override
    public int insertSelective(ProductLog record) {
        return productLogMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(ProductLog record) {
        return productLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ProductLog selectByUserIdAndCode(String userId, String code, String url) {
        LocalDate localDate = LocalDate.now();
        ProductLogExample productLogExample = new ProductLogExample();
        productLogExample.setOrderByClause("create_date desc");
        productLogExample.createCriteria().andDeletedEqualTo((short) 0).andUserIdEqualTo(userId)
                .andCodeEqualTo(code).andAccessUrlEqualTo(url).andCreateDateGreaterThanOrEqualTo(localDate.toDate()).andCreateDateLessThan(localDate.plusDays(1).toDate());
        List<ProductLog> productLogList = productLogMapper.selectByExample(productLogExample);
        if(productLogList != null && !productLogList.isEmpty())
            return productLogList.get(0);
        return null;
    }

    @Override
    public int updateHandleData(String pid) {
        ProductLogExample productLogExample = new ProductLogExample();
        ProductLog productLog = new ProductLog();
        productLog.setHandled((short) 1);
        productLogExample.createCriteria().andDeletedEqualTo((short) 0).andHandledEqualTo((short) 0).andIdEqualTo(pid);
        productLogMapper.updateByExampleSelective(productLog, productLogExample);
        return 0;
    }
}
